package com.businessportal.web.dataaccess.entities;

import org.hibernate.FetchMode;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

import javax.persistence.*;
import java.util.*;

/**
 * Created by Vadim on 6/20/14.
 */

@Entity
@Table( uniqueConstraints = { @UniqueConstraint(columnNames = "login") } )
public class Login {

    public enum ROLES {
        Sansey,
        Admin,
        Looser
    }


    @Id
    @GeneratedValue
    private Long id;

    private String login;
    private String password;
    private String fullName;

    private Boolean isConfirmed = false;

    @Enumerated(EnumType.STRING)
    private ROLES role = ROLES.Looser;

    @OneToMany(mappedBy = "login" ,cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private Set<Phone> phones = new HashSet<Phone>();

    @OneToMany(mappedBy = "login" ,cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private Set<Email> emails = new HashSet<Email>();

    @OneToMany(mappedBy = "login" ,cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private Set<Address> address = new HashSet<Address>();

    @OneToMany(mappedBy = "login" ,cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private Set<StudyApplication> studyApplication = new HashSet<StudyApplication>();

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @Sort(type=SortType.NATURAL)
    @JoinColumn(name="owner_id")
    private SortedSet<BlogEntity> blogs = new TreeSet<BlogEntity>();

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @Sort(type=SortType.NATURAL)
    @JoinColumn(name="owner_id")
    private SortedSet<Event> events = new TreeSet<Event>();


    public Login(){

    }

    public Login(Long id){
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public void addPhone(Phone newPhone){
        newPhone.setLogin(this);
        phones.add(newPhone);
    }

    public void addEmail(Email newEmail){
        newEmail.setLogin(this);
        emails.add(newEmail);
    }

    public void addAddress(Address newAddress){
        newAddress.setLogin(this);
        address.add(newAddress);
    }

    public void removeAddressById(long id){
        Iterator<Address> iterator = address.iterator();
        while(iterator.hasNext())
        {
            Address value = iterator.next();
            if (value.getId() == id)
            {
                iterator.remove();
                break;
            }
        }
    }

    public void addEvent(Event event){
        events.add(event);
    }

    public void addStudyApplication(StudyApplication application){
        application.setLogin(this);
        studyApplication.add(application);
    }

    public Set<BlogEntity> getBlogs() {
        return blogs;
    }

    public void AddBlog(BlogEntity newBlog){
        //newBlog.setOwner(this);
        blogs.add(newBlog);
    }

    public SortedSet<Event> getEvents() {
        return events;
    }

    public void addEvents(Event events) {
        this.events.add(events);
    }

    public Boolean getIsConfirmed() {
        return isConfirmed;
    }

    public void setIsConfirmed(Boolean isConfirmed) {
        this.isConfirmed = isConfirmed;
    }

    public ROLES getRole() {
        return role;
    }

    public void setRole(ROLES role) {
        this.role = role;
    }

    public Set<Address> getAddress() {
        return address;
    }

    public void setAddress(Set<Address> address) {
        this.address = address;
    }
}
